const { getFollowingList, getFollowerList, addFollowing, removeFollowing, getFollowingCount, getFollowerCount } = require('../../model/follow')

/**
 *  查询关注列表 
 */
const followingList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size } = req.query
    // 当前登录账号uid
    const { uid } = req.loginInfo;
    const map = page && size ? { page, size } : {}
    map.uid = uid
    const result = await getFollowingList(map)
    if (!result) {
        output.code = 500;
        output.msg = '查询数据失败'
    }
    output.data = result;
    res.send(output);
}

/**
 *  查询关注总数
 */
const followingTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    // 当前登录账号uid
    const { uid } = req.loginInfo;
    const result = await getFollowingCount(uid)
    output.data = result;
    res.send(output);
}



/**
 *  查询粉丝列表 
 */
const followerList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size } = req.query
    // 当前登录账号uid
    const { uid } = req.loginInfo;
    const map = page && size ? { page, size } : {}
    map.follow_uid = uid
    const result = await getFollowerList(map)
    if (!result) {
        output.code = 500;
        output.msg = '查询数据失败'
    }
    output.data = result;
    res.send(output);
}

/**
 *  查询粉丝总数
 */
const followerTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    // 当前登录账号uid
    const { uid } = req.loginInfo;
    const result = await getFollowerCount(uid)
    output.data = result;
    res.send(output);
}


/**
 *  添加关注 
 */

const followingAdd = async () => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    // 当前登录账号uid
    const { uid } = req.loginInfo;
    const { follow_uid } = req.body;
    const result = await addFollowing(uid, follow_uid)
    if (!result.insertId) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}

/**
 *  取消关注 
 */
const followingRemove = async () => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    // 当前登录账号uid
    const { uid } = req.loginInfo;
    const { follow_uid } = req.body;
    const result = await removeFollowing(uid, follow_uid)
    if (!result.insertId) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}

module.exports = {
    followingList, followerList, followingAdd, followingRemove, followerTotal, followingTotal
}